package com.guowen.myaccount.frag_history;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.guowen.myaccount.R;
import com.guowen.myaccount.adapter.AccountAdapter;
import com.guowen.myaccount.db.AccountBean;
import com.guowen.myaccount.db.DBManager;
import com.guowen.myaccount.utils.ChoiceDialog;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class YearFragment extends Fragment implements View.OnClickListener{

    private ImageView leftIv, rightIv;
    private TextView yearTv, yearTimeTv;
    private ListView recordLv;

    private int year, month;
    private List<AccountBean> mDatas;
    private AccountAdapter adapter;

    private BarChart mBarChart;
    private BarData mBarData;
    List<Integer> months;

    private RelativeLayout searchRl;

    List<BarEntry> yValues;

    public YearFragment() {
        // Required empty public constructor
    }

    // TODO: Rename and change types and number of parameters
    public static YearFragment newInstance() {
        YearFragment fragment = new YearFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_year, container, false);
        initView(view);
        initTime();
        initData();
        return view;
    }

    private void initData() {
        yearTv.setText(year+"");
        String yearTime = year+"-01-01至"+year+"-12-31";
        yearTimeTv.setText(yearTime);

        mDatas = new ArrayList<>();
        mDatas = DBManager.getAccountListOneYearFromAccounttb(year);
        adapter = new AccountAdapter(getContext(),mDatas);
        adapter.notifyDataSetChanged();
        recordLv.setAdapter(adapter);

        // y 轴数据
        yValues = new ArrayList<>();
        float[] y = new float[]{0,0};
        if (mDatas.size() != 0){
            searchRl.setVisibility(View.GONE);
            months = DBManager.selectAllDifferentMonthsFromAccounttbByYear(year);
            for (int month : months){
                float outMoney = DBManager.getSumMoneyOneMonth(year,month,0);
                float inMoney = DBManager.getSumMoneyOneMonth(year,month,1);
                yValues.add(new BarEntry(month,new float[]{outMoney,inMoney}));
            }
        }else {
            searchRl.setVisibility(View.VISIBLE);
        }

        // y 轴数据集
        BarDataSet barDataSet = new BarDataSet(yValues, "收入支出情况");
        // 设置 对应数据 颜色
        barDataSet.setColors(Color.RED, Color.GREEN);
        barDataSet.setStackLabels(new String[]{"支出","收入"});

        // 2.0 ---- mBarData = new BarData(xValues, barDataSet);
        mBarData = new BarData(barDataSet);
        mBarChart.setData(mBarData);
        mBarChart.notifyDataSetChanged();
        mBarChart.invalidate();


        initBarChart();
    }

    //设置 BarChart参数使得 BarChart 符合我们业务需求
    private void initBarChart() {
        // 设置 条形图 简介
//        Description description = new Description();
//        // 可以自定义 位置
//        // description.setPosition(200, 200);
//        // 也可以 根据 X 轴，Y 轴进行偏移量设置
//        description.setXOffset(50f);
//        description.setYOffset(10f);
//        description.setText("我是 description");
//        mBarChart.setDescription(description);
        mBarChart.getDescription().setEnabled(false);
        // 设置 是否可以缩放
        mBarChart.setScaleEnabled(false);

        // 设置 柱子的宽度
        // mBarData.setBarWidth(2f);

        // 获取 x 轴
        XAxis xAxis = mBarChart.getXAxis();
        // 设置 x 轴显示位置
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        // 取消 垂直 网格线
//        xAxis.setDrawGridLines(false);
        // 设置 x 轴 坐标旋转角度
        xAxis.setLabelRotationAngle(10f);
        // 设置 x 轴 坐标字体大小
        xAxis.setTextSize(10f);
        // 设置 x 坐标轴 颜色
        xAxis.setAxisLineColor(Color.BLACK);
        // 设置 x 坐标轴 宽度
        xAxis.setAxisLineWidth(2f);
        // 设置 x轴 的刻度数量
//        xAxis.setLabelCount(months.size());

        // 获取 右边 y 轴
        YAxis mRAxis = mBarChart.getAxisRight();
        // 隐藏 右边 Y 轴
        mRAxis.setEnabled(false);
        // 获取 左边 Y轴
        YAxis mLAxis = mBarChart.getAxisLeft();
        mLAxis.setAxisLineColor(Color.BLACK);
        mLAxis.setAxisLineWidth(2f);
        mLAxis.setAxisMinimum(0f);
        // 取消 左边 Y轴 坐标线
//        mLAxis.setDrawAxisLine(false);
        // 取消 横向 网格线
//        mLAxis.setDrawGridLines(false);
        // 设置 Y轴 的刻度数量
        mLAxis.setLabelCount(10);
    }

    private void initTime() {
        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH) + 1;
    }

    private void initView(View view) {
        leftIv = view.findViewById(R.id.frag_year_left);
        rightIv = view.findViewById(R.id.frag_year_right);
        yearTv = view.findViewById(R.id.frag_year_monthyear);
        yearTimeTv = view.findViewById(R.id.frag_year_yeartime);
        recordLv = view.findViewById(R.id.frag_year_lv);
        mBarChart = view.findViewById(R.id.frag_year_bar);
        searchRl = view.findViewById(R.id.frag_year_null);

        leftIv.setOnClickListener(this);
        rightIv.setOnClickListener(this);
        yearTv.setOnClickListener(this);
        recordLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ChoiceDialog dialog = new ChoiceDialog(getContext(), position+1, mDatas, adapter);
                dialog.show();
                dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        initData();
                    }
                });
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.frag_year_left:
                year -= 1;
                initData();
                break;
            case R.id.frag_year_right:
                year += 1;
                initData();
                break;
            case R.id.frag_year_monthyear:
                new DatePickerDialog(getContext(), AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year1, int month, int dayOfMonth) {
                        year = year1;
                        initData();
                    }
                },year,month,1).show();
                break;
        }
    }
}